package com.kt.app.ui.components


import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument

@Composable
fun NavigationSample() {
    val navController = rememberNavController()
    NavHost(navController, startDestination = "First") {
        composable("First"){
            FirstScreen{
                navController.navigate("Second")
            }
        }
        composable("Second/{name}"){
            val name = it.arguments?.getString("name") // 传参
            SecondScreen {
                navController.navigate("Second")
            }
        }
        composable("Third?name={name}",  arguments = listOf(
            navArgument("name"){
                nullable=false
                defaultValue = "kt"
            }
        )){
            val name:String = it.arguments?.getString("name").toString()
            ThirdScreen(name) {
                navController.popBackStack()
            }
        }
    }


}

@Composable
fun FirstScreen(routeFun:()->Unit) {
    Column {
        Text("FirstScreen")
        Button(onClick = {
            routeFun()
        }) { Text("Go to SecondScreen") }
    }
}

@Composable
fun SecondScreen(routeFun:()->Unit) {
    Column {
        Text("SecondScreen")
        Button(onClick = {
            routeFun()
        }) { Text("Go to ThirdScreen") }
    }
}

@Composable
fun ThirdScreen(name:String,routeFun:()->Unit) {
    Column {
        Text("ThirdScreen $name")
        Button(onClick = {
            routeFun()
        }) { Text("back to ThirdScreen") }
    }
}

@Preview
@Composable
fun NavigationSamplePreview() {
    NavigationSample()
}

